<template>
    <div>
        <el-card class="mb">
            <el-input placeholder="请输入关键词搜索" v-model="queryform.keyword"
                style="width: 200px;margin-right: 10px;"></el-input>
            <el-button type="primary" @click="search()" plain>查询</el-button>
            <el-button type="info" @click="research()" plain>重置</el-button>
            <el-button type="warning" style="float:right" @click="add()" plain>新增</el-button>
        </el-card>
        <el-card>
            <div class="table">
                <el-table :data="tableData" strip>

                    <el-table-column type="selection" width="55" align="center"></el-table-column>
                    <el-table-column prop="id" label="序号" width="70" align="center" sortable></el-table-column>
                    <el-table-column prop="gname" label="名称"></el-table-column>
                    <el-table-column prop="price" label="价格"></el-table-column>
                    <el-table-column prop="discount" label="折扣"></el-table-column>
                    <el-table-column prop="img" label="图片">
                        <template v-slot="scope">
                            <div style="display: flex; align-items: center">
                                <el-image style="width: 40px; height: 40px;" v-if="scope.row.img" :src="scope.row.img"
                                    :preview-src-list="[scope.row.img]"></el-image>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="descr" label="描述"></el-table-column>
                    <el-table-column prop="origin" label="原材料"></el-table-column>
                    <el-table-column prop="taste" label="口味"></el-table-column>
                    <el-table-column prop="specs" label="规格"></el-table-column>
                    <el-table-column prop="date" label="上架日期"></el-table-column>
                    <el-table-column prop="status" label="上架状态">
                        <template v-slot="scope">
                            <el-tag type="success" v-if="scope.row.status === '上架'">上架</el-tag>
                            <el-tag type="warning" v-if="scope.row.status === '下架'">下架</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="商家"></el-table-column>
                    <el-table-column prop="cname" label="分类"></el-table-column>
                    <el-table-column label="操作" align="center" width="180">
                        <template v-slot="scope">
                            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <div class="pagination">
                    <el-pagination background @current-change="handleCurrentChange" :current-page="queryform.pageNum"
                        :page-sizes="[5, 10, 20]" :page-size="queryform.pageSize" layout="total, prev, pager, next"
                        :total="total">
                    </el-pagination>
                </div>
            </div>

        </el-card>
        <el-dialog title="商品信息" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
            <el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">
                <el-form-item label="名称" prop="gname">
                    <el-input v-model="form.gname" placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model="form.price" placeholder="价格"></el-input>
                </el-form-item>
                <el-form-item label="折扣" prop="discount">
                    <el-input v-model="form.discount" placeholder="折扣: 1表示不打折，折扣可以设置小数"></el-input>
                </el-form-item>
                <el-form-item label="图片" prop="img">
                    <el-upload action="/files/upload" :headers="{ token: user.token }"
                    :file-list="fileList"
                        :on-success="handleFileSuccess">
                        <el-button type="primary">上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item label="描述" prop="descr">
                    <el-input v-model="form.descr" placeholder="描述"></el-input>
                </el-form-item>
                <el-form-item label="原材料" prop="origin">
                    <el-input v-model="form.origin" placeholder="原材料"></el-input>
                </el-form-item>
                <el-form-item label="口味" prop="taste">
                    <el-input v-model="form.taste" placeholder="口味"></el-input>
                </el-form-item>
                <el-form-item label="规格" prop="specs">
                    <el-input v-model="form.specs" placeholder="规格"></el-input>
                </el-form-item>
                <el-form-item label="上架日期" prop="date">
                    <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd" v-model="form.date"
                        style="width: 100%"></el-date-picker>
                </el-form-item>
                <el-form-item label="上架状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio label="上架"></el-radio>
                        <el-radio label="下架"></el-radio>
                    </el-radio-group>
                </el-form-item>
                
                <el-form-item label="商家" prop="businessId" v-if="user.role == 'ADMIN'">
                    <el-select style="width: 100%" v-model="form.businessId" @change="selectca">
                        <el-option v-for="item in sjlist" :key="item.id" :value="item.id"
                            :label="item.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="分类" prop="categoryId">
                    <el-select style="width: 100%" @change="selectcaf" v-model="form.categoryId">
                        <el-option v-for="item in categoryList" :key="item.id" :value="item.id"
                            :label="item.cname"></el-option>
                    </el-select>
                </el-form-item>


            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="fromVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import service from '@/request';

export default {
    data() {
        return {
            fileList:[],
            tableData: [],  // 所有的数据
            pageNum: 1,   // 当前的页码
            pageSize: 10,  // 每页显示的个数
            total: 0,
            username: null,
            token: "",
            name: null,
            fromVisible: false,
            avatar: "",
            queryform: {
                keyword: "",
                pageNum: 1,
                pageSize: 10
            },
            license: "",
            form: {
                avatar: "",
                license: ""
            },
            user: {},
            rules: {

                cname: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                ],

                // business_id: [
                //     { required: true, message: '请选择商家类型', trigger: 'blur' },
                // ]
            },
            ids: [],
            businessList:[],
            categoryList:[],
            sjlist: [],
        }
    },
    mounted() {
        this.token = window.localStorage.getItem("token")
        this.user = JSON.parse(window.localStorage.getItem("userinfo"))
        console.log(this.user)
        this.getall()
        this.getlist()
        if(this.user.role == 'BUSINESS'){
            this.selectca(this.user.id)
        }
    },
    methods: {
      async  selectca(e){
            console.log(e)
            this.form.categoryId=""
            const res = await service.post("/Category/selectById?id="+e);
            console.log(res)
            this.categoryList=res.data

        },
        selectcaf(e){
this.$forceUpdate()
        },
        handleFileSuccess(e){
            this.form.img=e.data
        },
        async getall() {
            const res = await service.get("/Business/all");
            console.log(res)
            this.sjlist = res.data.list
        },

        del(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const res = await service.post("/Goods/delete?id=" + id)
                console.log(res)
                if (res.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getlist()
                }

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        force(e) {
            this.form.timeRange = e;
            this.$forceUpdate();
        },
        handleEdit(row) {
            this.form = row;
            this.avatar = row.avatar;
            this.license = row.license;
            this.form.timeRange = row.time_range
            this.fileList=[];
            this.fileList.push({
                name:row.gname,
                url:row.img
            })
            this.form.businessId=row.business_id

            this.selectca(this.form.businessId)
            this.form.categoryId=row.category_id
            this.fromVisible = true
        },
        search() {
            this.queryform.pageNum = 1;
            this.queryform.pageSize = 10;
            this.getlist()
        },
        research() {
            this.queryform.keyword = ""
            this.queryform.pageNum = 1;
            this.queryform.pageSize = 10;
            this.getlist()

        },
        async getlist() {
            if (this.user.role == 'BUSINESS') {
                this.queryform.businessId = this.user.id
            }
            const res = await service.post("/Goods/list", this.queryform);
            console.log(res);
            if (res.code == 200) {
                this.tableData = res.data.rows;
                this.total = res.data.total;
            }


        },
        add() {
            this.avatar = ""
            this.license = "";
            this.fileList=[];
            this.form = {}
            this.fromVisible = true

        },
        handleLicenseSuccess(e) {

        },
        async save() {

            if (this.user.role == 'BUSINESS') {
                this.form.businessId = this.user.id
            
            } else {
                // this.form.business_id = this.form.business_id
            }
            if (!this.form.id) {
                const res = await service.post("/Goods/add",this.form);
                if (res.code == 200) {
                    this.fromVisible = false
                    this.$message.success("操作成功")
                    this.getlist()
                }
            } else {
                const res = await service.post("/Goods/update", this.form);
                if (res.code == 200) {
                    this.fromVisible = false
                    this.$message.success("操作成功")
                    this.getlist()
                }
            }




        },
        handleCurrentChange(e) {

        },
        handleAvatarSuccess(e) {
            console.log(e)
            this.avatar = e.data;
            console.log(this.form.avatar)
        },
        handleAvatarSuccess1(e) {

            this.license = e.data;
        }
    }
}
</script>
<style scoped>
.mb {
    margin-bottom: 10px;
}
</style>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px !important;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>